﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace CoolERP_Common
{
    public static class StringHelper
    {
        /// <summary>
        /// 移除HTML格式
        /// </summary>
        /// <param name="Htmlstring"></param>
        /// <returns></returns>
        public static string RemoveHtml(string Htmlstring)
        {
            //删除脚本
            Htmlstring = Regex.Replace(Htmlstring, @"(?s)<script.*?>.*?</script>", "", RegexOptions.IgnoreCase);
            //删除样式
            Htmlstring = Regex.Replace(Htmlstring, @"(?s)<style.*?>.*?</style>", "", RegexOptions.IgnoreCase);
            //删除图片
            //Htmlstring = Regex.Replace(Htmlstring, @"(?s)<img.*?>", "", RegexOptions.IgnoreCase);
            //删除HTML
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);

            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);

            Htmlstring = Htmlstring.Replace("<", "〈");
            Htmlstring = Htmlstring.Replace(">", "〉");
            Htmlstring = Htmlstring.Replace("\r\n", "");
            Htmlstring = Htmlstring = Htmlstring.Replace("&nbsp;", "");
            Htmlstring = Htmlstring.Replace("&ldquo;", "“");
            Htmlstring = Htmlstring.Replace("&rdquo;", "”");
            Htmlstring = Htmlstring.Replace("&amp;", "&");
            Htmlstring = System.Web.HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();

            return Htmlstring;
        }

        /// <summary>
        /// 截取指定字符串长度
        /// </summary>
        /// <param name="inputString"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string CutString(object inputString, int length)
        {
            return CutString(inputString.ToString(), length, true);
        }

        /// <summary>
        /// 截取指定字符串长度
        /// </summary>
        /// <param name="inputString"></param>
        /// <param name="length"></param>
        /// <param name="IsRemoveHtml"></param>
        /// <returns></returns>
        public static string CutString(object inputString, int length, bool IsRemoveHtml)
        {
            if (inputString == null)
                return string.Empty;
            string formatString = inputString.ToString().Trim();
            if (IsRemoveHtml)
                formatString = RemoveHtml(formatString);
            byte[] myByte = System.Text.Encoding.Default.GetBytes(formatString);
            if (myByte.Length > length)
            {
                //截取操作
                string resultStr = "";
                for (int i = 0; i < formatString.Length; i++)
                {
                    resultStr += formatString.Substring(i, 1);
                    int tempNum = System.Text.Encoding.Default.GetBytes(resultStr).Length;
                    if (tempNum >= length * 2)
                    {
                        if (myByte.Length > tempNum)
                            resultStr += "...";
                        break;
                    }
                }
                return resultStr;
            }
            else
            {
                return formatString;
            }
        }

        public static string GetLengthStringWithChar(string str, int length, char c)
        {
            if (str.Length < length)
            {
                str = str.PadRight(length, c);
            }
            return str;
        }
    }
}
